<template>
	<view class="near">
		<u-navbar id="tabbars" :is-back="false" :border-bottom="false" back-icon-color="#fff" title="附近氧站"
			title-size="36" title-color="#fff" :background="background">

			<view @click="goback" class="slot-wrap">
				<image  class="back" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/21.png"
					mode=""></image>
			</view>
		</u-navbar>

		<view :style="{'top': totalHeight+'px'}" class="decoration">
			<image class="imgs" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/c1.png" mode=""></image>
			<view style="margin: 0 27rpx; background: #EDF6FF;">
				<u-search :show-action="true" bg-color="#fff" shape="square" @search="search" @custom="search"
					placeholder="请输入关键字" v-model="keyword"></u-search>
			</view>
		</view>


		<view  class="content">
			<!-- <image :style="{'top': totalHeight+'px'}" class="deco" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/14.png" mode="widthFix"></image> -->
			<view  class="info">
				<view style="padding-top: 65px;"  class="list" v-if="list1.length">
					<view @click="gosolt(item)" v-for="item in list1" class="item" >
						<view class="status">
							<text class="doing" v-if="item.use_state=='营业中'">{{item.use_state}}</text>
							<text v-else>{{item.use_state}}</text>
						</view>
						<view class="iteminner">
							<!-- <img :class="item.online_state=='在线'?'online':'notonline'"  class="img" 
							src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/20.png" alt=""> -->
							<!-- <img :class="item.online_state=='在线'?'online':'notonline'"  class="img"
							:src="item.icon_url" alt=""> -->
							
							<u-upload  width="144rpx" height="144rpx" :max-count="1" :deletable="false" :show-progress="false" :file-list=" [{url:item.icon_url}]" ></u-upload>
							<view  class="detail">
								<view  class="name">
									<view class="left">
										{{item.device_name}}
									</view>
									<!-- <view class="right">
										距离{{parseInt(item.juli/1000)}}KM

									</view> -->
								</view>
								<view class="mid">
									<view  class="left">
										<view class="addr">
											<img class="icon"
												src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/16.png" alt="">
											<view style="flex: 1;" class="">
												{{item.address}}
											</view>
										</view>
										<view class="time">
											<img class="icon"
												src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/17.png" alt="">
											{{item.business_hours}}
										</view>
									</view>
									<view   class="right">
										<image @click.stop="goDetail(item)" class="dh" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/15.png"
											mode="widthFix"></image>
											
											<view class="distance">
												距离{{formatDistance(item.juli)}}
												
												<!-- {{parseInt(item.juli/1000)}}KM -->
											</view>
									</view>
								</view>

							</view>
						</view>
						<view  class="bottoms">
							<view class="left">
								<!-- 共有12个 -->
								<text v-if="item.online_state=='在线'" style="color: #08B878;">{{item.online_state}}</text>
								<text v-else style="color: #999999;" >{{item.online_state}}</text>
							</view>
							<view class="right">
								<view class="same2">
									<image class="icon" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/18.png"
										mode=""></image>
									可租借 {{item.can_buy_count}} 罐氧气
								</view>
								<view class="same1">
									<image class="icon" src="https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/19.png"
										mode=""></image>
									可归还 {{item.empty_count}} 个空槽
								</view>
							</view>
						</view>
					</view>
					<uni-load-more :status="status"></uni-load-more>
				</view>
				<view v-else class="empty" style="padding-top: 150px;">
					<view v-if="!list1.length && senDate" class="emptyAddress-box">
						<u-empty text="暂无数据" mode="news"></u-empty>
					</view>
				</view>


				
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				deviceId:"",
				keyword: "",
				totalHeight: 0, //总高度  
				background: {

					background: 'url(https://ysy001.oss-cn-beijing.aliyuncs.com/ysyIco/9.png) no-repeat',
					// 还可以设置背景图size属性
					backgroundSize: 'cover',
					// 渐变色
					// backgroundImage: 'linear-gradient(180deg, #417CF4 0%, #84ACFF 100%)'
				},
				total: null, //总共多少条数据
				formData: {
					pageSize: 15, //每页10条数据
					page: 1, //第几页
					topic: "",
				},
				status: "more",
				senDate: false,
				list1: [],
			}
		},
		onReady() {
			uni.hideTabBar();
			uni.createSelectorQuery().select('#tabbars').boundingClientRect((rect) => {
				console.log(rect.height)
				this.totalHeight = rect.height
			}).exec()
		},
		
		onLoad(e) {
			this.type=e.type
			this.deviceId = e.deviceId
			this.getlist()
			// deviceId="+this.deviceId
		},
		onReachBottom() {
			let allTotal = this.formData.page * this.formData.pageSize;
			if (allTotal < this.total) {
				//当前条数小于总条数 则增加请求页数
				this.status = "loading"; //加载中状态
				this.formData.page++;

				this.getlist();

			} else {
				this.status = "noMore"; //加载完状态
				console.log("已加载全部数据");
			}
		},
		methods: {
			gosolt(item){
				
				
			if(!this.vuex_token){
				uni.navigateTo({
					url:"/pages/sys/main/login"
				})
				
				return
			}
				
				console.log(item,33333333333);
				uni.navigateTo({
					url:"/pages/sys/scar/zu?deviceId="+item.device_id
				})
			},
			goDetail(val) {
				uni.openLocation({
					latitude: val.latitude*1,
					longitude: val.longitude*1,
					name: val.address,
					success() {
						console.log('success');
					}
				});
			},
			goback() {
				
				console.log(this.type,44444444444);
				if(this.type){
					
					// 跳转需要携带deviceId
					uni.navigateTo({
						url:"/pages/sys/scar/index?deviceId="+this.deviceId
						// url:"/pages/sys/scar/index?deviceId="+this.deviceId
					})
				}else{
					uni.reLaunch({
						url: "/pages/sys/main/index"
					})
				}
				
			},
			
			formatDistance(distance) {
			  if (distance < 1000) {
			    return distance.toFixed(0) + 'm';
			  } else {
			    return (distance / 1000).toFixed(2) + 'km';
			  }
			},

			search() {
				this.formData.page = 1
				this.list1 = []
				this.getlist()
			},
			getlist() {
				this.senDate = false;
				this.$u.api.nearbyDevice({
						page: this.formData.page,
						limit: this.formData.pageSize,
						longitude: this.vuex_Long,
						latitude: this.vuex_Lat,
						key: this.keyword,
					})
					.then((res) => {

						this.senDate = true;
						if (res.code == 0) {

							console.log(res.data, 666666);
							this.total = res.data.total;
							const newlist = res.data.list;
							this.list1.push(...newlist);
							
							
							
							

							console.log(this.list1, 455333);

							if (this.list1.length < this.formData.pageSize) {
								this.status = "noMore"; //加载完状态
							}
						} else {
							console.log("请求数据失败");
						}
					});
			}
		},
	}
</script>


<style>
	page{
		background: #EDF6FF;
	}
</style>
<style scoped lang="scss">
	.notonline{
		border: 2rpx solid #B6B6B6;
	}
	.online{
		border: 2rpx solid #4891FF;
	}
	.distance{
		font-size: 11px;
		color: #999;
		margin-top: -4px;
	}
	.status{
		position: absolute;
		right: 0;
		top: 0;
		width: 129rpx;
		height: 43rpx;
		
		text{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 129rpx;
			height: 43rpx;
			background: #E8E8E8;
			color: #797979;
			font-size: 12px;
			border-radius: 0 0 0 32rpx;
			
			&.doing{
				background: #A5E9FF;
				color: #129AC6;
				
			}
		}
	}
	.slot-wrap{
		position: relative;
		z-index: 99999;
		width: 90px;
		height: 40px;
		// background: red;
		display: flex;
		align-items: center;
		
	}
	.decoration {
		width: 100%;
		height: 35rpx;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 88;

		.imgs {
			width: 100%;
			height: 35rpx;
			display: block;
		}
	}

	.back {
		width: 30rpx;
		height: 46rpx;
		margin-left: 40rpx;
	}

	.content {

		position: relative;

		.deco {
			width: 100%;
			height: 35rpx;
			position: fixed;
			left: 0;
			right: 0;
			z-index: 88;

			.imgs {
				width: 100%;
				height: 35rpx;
				display: block;
			}
		}

		.info {
			margin: 0 27rpx;
			min-height: calc(100vh - 50px);

			.list {
				padding-top: 16px;

				.item {
					position: relative;
					padding: 38rpx 20rpx 36rpx 36rpx;
					background: #FFFFFF;
					border-radius: 11rpx 11rpx 11rpx 11rpx;
					margin-bottom: 32rpx;
				}

				.bottoms {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.left {
						width: 144rpx !important;
						text-align: center;
						font-size: 24rpx;
						color: #a4a4a4;
						margin-right: 29rpx;
					}

					.right {
						flex: 1;
						display: flex;
						align-items: center;

						.same2 {
							display: flex;
							align-items: center;

							padding: 6rpx 22rpx 6rpx 11rpx;
							background: #E6FFE5;
							border: 1rpx solid #60D990;
							border-radius: 10rpx;
							font-size: 21rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #08B878;
							margin-right: 10rpx;

							.icon {
								width: 22rpx;
								height: 19rpx;
								margin-right: 12rpx;
							}
						}

						.same1 {
							padding: 6rpx 22rpx 6rpx 11rpx;
							background: #DDE7FF;
							border: 1rpx solid #81A4FF;
							border-radius: 10rpx;
							font-size: 21rpx;
							font-family: PingFang SC-Medium, PingFang SC;
							font-weight: 500;
							color: #1B6BE3;

							.icon {
								width: 22rpx;
								height: 19rpx;
								margin-right: 15rpx;
							}
						}
					}
				}

				.iteminner {

					// overflow: hidden;
					display: flex;

					.img {
						width: 144rpx !important;
						height: 144rpx !important;
						border-radius: 11rpx 11rpx 11rpx 11rpx;
						margin-right: 29rpx;
					}

					.detail {
						width: 500rpx;
						flex: 1;

						.name {
							
							display: flex;
							align-items: center;
							justify-content: space-between;

							.left {
								font-size: 32rpx;
								color: #333;
							}

							.right {
								padding-top: 24rpx;
								font-size: 24rpx;
								color: #999;
							}
						}

						.mid {
							padding: 25rpx 0 0 0;
							display: flex;

							.left {
								flex: 1;
								font-size: 23rpx;
								color: #333333;

								.addr {
									padding-bottom: 17rpx;
									display: flex;
									align-items: center;

									.icon {
										width: 24rpx;
										height: 29rpx;
										margin-right: 13rpx;
									}
								}

								.time {
									padding-bottom: 35rpx;
									display: flex;
									align-items: center;

									.icon {
										width: 24rpx;
										height: 24rpx;
										margin-right: 13rpx;

									}
								}
							}

							.right {
								margin-top: -36rpx;
								display: flex;
								flex-direction: column;
								    align-items: flex-end;

								.dh {
									width: 90rpx;
									height: 90rpx;
								
									// margin: 0 auto;
								}
							}
						}
					}

				}
			}
		}
	}
</style>
